<?php
    header("Content-Type: text/html; charset=iso-8859-1");
    include "/includes/session.php";
    include "/includes/validaUsuario2.php";
    require_once "/includes/DAOs/UserDAO.php";
?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <!-- INICIO CALENDARIO -->
		<script type="text/javascript">
			function validarFormulario(){
				var nombre_evento = document.getElementById('nombre_evento');
				var fecha_inicio = document.getElementById('fecha_inicio');
				var fecha_termino = document.getElementById('fecha_termino');
				var direccion_evento = document.getElementById('direccion_evento');
				var precios = document.getElementById('precios');
				var categoria = document.getElementById('id_categoria');
				var lat = document.getElementById('lat');
				var lon = document.getElementById('lon');
				
				if(nombre_evento.value == "" || fecha_inicio.value == "" || fecha_termino.value == "" || precios.value == "" || (direccion_evento.value == "" && (lat.value == "" || lon.value == ""))){
					alert('Rellene todos los campos');
				}
				else{
					fecha = fecha_inicio.value.split("-");
					fechaInicio = new Date();
					fechaInicio.setFullYear(fecha[0], fecha[1], fecha[2]);
					fecha = fecha_termino.value.split("-");
					fechaTermino = new Date()
					fechaTermino.setFullYear(fecha[0],fecha[1],fecha[2]);
					fechaActual = new Date();
					if(fechaTermino < fechaActual || fechaTermino < fechaInicio){
						alert('Error en Fecha Final');
					}
					else{
						document.forms["formularioEvento"].submit();
					}
				}
			}
		</script>
		
		<!-- INICIO CALENDARIO -->
		<script language="javascript" src="js/popcalendar.js"></script>		
		<!-- FIN CALENDARIO -->
		
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
		<script id="jqueryui" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" defer async></script>
		<script src="//www.google.com/jsapi?key=AIzaSyCZfHRnq7tigC-COeQRmoa9Cxr0vbrK6xw"></script>
		<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
		<script>
			var map;
			var geocoder; // = new google.maps.Geocoder();
			var tempMarker = new google.maps.Marker();
			var locationIcon = new google.maps.MarkerImage(
				"https://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png",
				new google.maps.Size(12,20),
				new google.maps.Size(6,20)
			);
			var newShadow = new google.maps.MarkerImage(
				"https://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_shadow.png",
				new google.maps.Size(22,20),
				new google.maps.Point(13,13)
			);
			function initialize() {
				geocoder = new google.maps.Geocoder();
				var mapOptions = {
					zoom: 16,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
				// Try HTML5 geolocation
				if(navigator.geolocation) {
					navigator.geolocation.getCurrentPosition(function(position) {
						var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
						map.setCenter(pos);
					}, function() {
						handleNoGeolocation(true);
					});
				} else {
					// Browser doesn't support Geolocation
					handleNoGeolocation(false);
				}
				
				tempMarker.setOptions({
					icon: locationIcon,
					shadow: newShadow,
					draggable: true
				});
				
				google.maps.event.addListener(map, "click", showTempMarker);
				
				function showTempMarker(e) {
					tempMarker.setPosition(e.latLng);
					document.getElementById('lat').value = e.latLng.lat();
					document.getElementById('lon').value = e.latLng.lng();
					google.maps.event.addListener(tempMarker, "drag", function() {
						document.getElementById('lat').value = tempMarker.getPosition().lat();
						document.getElementById('lon').value = tempMarker.getPosition().lng();
					});
					tempMarker.setMap(map);
				}
			}
			
			function handleNoGeolocation(errorFlag) {
				if (errorFlag) {
					var content = 'Error: The Geolocation service failed.';
				} else {
					var content = 'Error: Your browser doesn\'t support geolocation.';
				}
				var options = {
					map: map,
					position: new google.maps.LatLng(-33.034355,-71.596313),
					content: content
				};
				var infowindow = new google.maps.InfoWindow(options);
				map.setCenter(options.position);
			}
			
			function showAddress(address) {
				geocoder.geocode({'address': address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						var pos = results[0].geometry.location;
						map.setCenter(pos);
						tinyMarker = new google.maps.Marker({
							position: pos,
							map: map,
							icon: locationIcon,
							shadow: newShadow,
							draggable: true
						});
						document.getElementById('lat').value = pos.lat();
						document.getElementById('lon').value = pos.lng();
					} else {
					}
				});
			}
			
			function buscar(){
				var aux = document.getElementById('address');
				var address = aux.value + ", Chile";
				showAddress(address);
			}
			
			google.maps.event.addDomListener(window, 'load', initialize);
		</script>
		<title>Go Now - Usuario</title>
    </head>
    <body>
	 <div class="image" style="width:968px;">		
			  <div class="user">
                <?php
                    $userDAO = new UserDAO();
                    $resultado = $userDAO->getNombreUsuario($_SESSION['id_user']);
                    list($nombre) = $userDAO->fetch($resultado);
                ?>
                <p>Usuario: <?php echo $nombre ?> <a href="includes/logout2.php">salir</a></p>
            </div>
			
						<a href="http://localhost/gonow/indexUser.php">
			<img  src="css/logo.png"  alt="logo"  style="margin-left:35px;"/> </a> 
			</div>
        <div id="contenido" class="content" style="min-height:850px;">
		  <div id="sidebar">
		  <ul>
		        <li><a href="indexUser.php">Mis Eventos</a></li> 
                <li style="background:#659C08;"><a href="agregarEvento.php">Agregar Evento</a></li>       
                <li><a href="misDatos.php">Mis Datos</a></li>
          </ul>				
          </div>
          
		  <div style="float:right; width:750px;">
			<div style="float:right; width:420px;">
		     <div style="float: right; margin-right:20px; margin-top:40px; margin-bottom:20px;">
					<table>	
						<tr>
							<td><input type="text" id="address" name="address" /></td>
							<td><input style="width: 100px; margin-top: -1px;" class="green_btn" onClick="buscar();" type="submit" id="Buscar" name="Buscar" value="Buscar"/></td>
						</tr>
					</table>
			</div>	
				<div id="map_canvas" style="  margin-right:20px; height: 450px; width: 400px;"></div>
			</div>
	  
            
			<div class="agregar-box" style="float:left;">
                <form name="formularioEvento" id="formularioEvento" action="includes/controllers/userController.php?action=agregarEvento" method="post">
                        <p>Nombre del Evento</p>
                        <input type="text" name="nombre_evento" id="nombre_evento" />
						<p>Fecha Inicio</p>
                        <input type="text" name="fecha_inicio" id="fecha_inicio" onClick="popUpCalendar(this,formularioEvento.fecha_inicio,'yyyy-mm-dd')" style="margin-bottom:10px;"/>
						<select id="hora_inicio" name="hora_inicio">
							<option value="00:00:00">00:00 Hrs</option>
							<option value="01:00:00">01:00 Hrs</option>
							<option value="02:00:00">02:00 Hrs</option>
							<option value="03:00:00">03:00 Hrs</option>
							<option value="04:00:00">04:00 Hrs</option>
							<option value="05:00:00">05:00 Hrs</option>
							<option value="06:00:00">06:00 Hrs</option>
							<option value="07:00:00">07:00 Hrs</option>
							<option value="08:00:00">08:00 Hrs</option>
							<option value="09:00:00">09:00 Hrs</option>
							<option value="10:00:00">10:00 Hrs</option>
							<option value="11:00:00">11:00 Hrs</option>
							<option value="12:00:00">12:00 Hrs</option>
							<option value="13:00:00">13:00 Hrs</option>
							<option value="14:00:00">14:00 Hrs</option>
							<option value="15:00:00">15:00 Hrs</option>
							<option value="16:00:00">16:00 Hrs</option>
							<option value="17:00:00">17:00 Hrs</option>
							<option value="18:00:00">18:00 Hrs</option>
							<option value="19:00:00">19:00 Hrs</option>
							<option value="20:00:00">20:00 Hrs</option>
							<option value="21:00:00">21:00 Hrs</option>
							<option value="22:00:00">22:00 Hrs</option>
							<option value="23:00:00">23:00 Hrs</option>
						</select>
						<p>Fecha Termino</p>
                        <input type="text" name="fecha_termino" id="fecha_termino" onClick="popUpCalendar(this,formularioEvento.fecha_termino,'yyyy-mm-dd')" style="margin-bottom:10px;"/>
						<select id="hora_termino" name="hora_termino">
							<option value="00:00:00">00:00 Hrs</option>
							<option value="01:00:00">01:00 Hrs</option>
							<option value="02:00:00">02:00 Hrs</option>
							<option value="03:00:00">03:00 Hrs</option>
							<option value="04:00:00">04:00 Hrs</option>
							<option value="05:00:00">05:00 Hrs</option>
							<option value="06:00:00">06:00 Hrs</option>
							<option value="07:00:00">07:00 Hrs</option>
							<option value="08:00:00">08:00 Hrs</option>
							<option value="09:00:00">09:00 Hrs</option>
							<option value="10:00:00">10:00 Hrs</option>
							<option value="11:00:00">11:00 Hrs</option>
							<option value="12:00:00">12:00 Hrs</option>
							<option value="13:00:00">13:00 Hrs</option>
							<option value="14:00:00">14:00 Hrs</option>
							<option value="15:00:00">15:00 Hrs</option>
							<option value="16:00:00">16:00 Hrs</option>
							<option value="17:00:00">17:00 Hrs</option>
							<option value="18:00:00">18:00 Hrs</option>
							<option value="19:00:00">19:00 Hrs</option>
							<option value="20:00:00">20:00 Hrs</option>
							<option value="21:00:00">21:00 Hrs</option>
							<option value="22:00:00">22:00 Hrs</option>
							<option value="23:00:00">23:00 Hrs</option>
						</select>
						<p>Direccion del Evento</p>
                        <input type="text" name="direccion_evento" id="direccion_evento" />
						<p>Precios</p>
                        <input type="text" name="precios" id="precios" />
						<p>Categoria</p>
                        <select name="id_categoria" id="id_categoria">
											<?php
											$userDAO = new UserDAO();
											$result = $userDAO->getCategorias();
											while(list($id_categoria, $nombre_categoria) = $userDAO->fetch($result)){
											?>
												<option value="<?php echo $id_categoria; ?>"><?php echo $nombre_categoria; ?></option>
											<?php
											}	
											?>
						</select>
						<p>Descripcion: </p>
			            <textarea cols="28" rows="5" id="descripcion" name="descripcion"></textarea>
						<p>Latitud </p>
                        <input type="text" id="lat" name="lat" size="15" />
						<p>Longitud: </p>
						<input type="text" id="lon" name="lon" size="15" />
                </form>
			</div>
				<input class="green_btn" type="button" onClick="javascript:validarFormulario();" value="Agregar Evento" style="width:170px; margin-left:110px; margin-top:30px; "/></td>
        

		</div>
		</div>
    </body> 
<footer>		
		<p> Proyecto Seminario II Telem&aacute;tica 2012 &copy Gonz&aacute;lez - Orozco - Pino - Villarreal</p>
    </footer>
</html>
